package com.rnyoudaiapp;

import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;

import com.facebook.react.bridge.ActivityEventListener;
import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.LifecycleEventListener;
import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.modules.core.DeviceEventManagerModule;
import com.rnyoudaiapp.util.encryptutil.AESCipher;
import com.rnyoudaiapp.util.encryptutil.RSA;
import com.rnyoudaiapp.util.encryptutil.RandomUtil;

import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Nullable;

/**
 * Created by WangYu on 2017/8/23.
 */

public class MyNativeMoudle extends ReactContextBaseJavaModule implements ActivityEventListener, LifecycleEventListener {
    private ReactApplicationContext context;
    public static final String publicKey =
            "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQD5/QmQqMqisMt01T23UZHBsU9N" +
            "DAMkUdzgAct6J7TZVKVzgnJ4/2fjvf7rDzDdJekzakShAgo7QOzfbA0Fvi3Aq/Gk" +
            "YEOa3LhlIcTEaemjwBQGzTP4Gfcq/2wQGZ59UZRGQYdi833t0HzOqQMzaNYXieMy" +
            "z0rIA32/bt/6In+rCwIDAQAB";

    public MyNativeMoudle(ReactApplicationContext reactContext) {
        super(reactContext);
        context = reactContext;
        context.addLifecycleEventListener(this);
    }

    @Override
    public String getName() {
        return "MyNativeMoudle";
    }

    @Nullable
    @Override
    public Map<String, Object> getConstants() {
        //可以给rn提供常量
        Map<String, Object> map = new HashMap<>();
        map.put("url", "www.url.com");
        return map;
    }

    @ReactMethod
    public void rnCallNative(String msg) {
//        Toast.makeText(context,msg,Toast.LENGTH_SHORT).show();
//        context.addActivityEventListener(this);
//        Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
//        context.startActivityForResult(intent, 0, new Bundle());

    }

    /**
     * 普通请求的参数加密
     * @param param
     * @param callback
     * @throws Exception
     */
    @ReactMethod
    public void encrypt(String param,Callback callback) throws Exception {
        Log.i("wangyu", "encrypt: "+param);
        //生成16位字母或者数字字符串,用rsa加密
        String apiKey= RandomUtil.getRandom(16);
        String apiKeyEnc = RSA.encrypt(apiKey,publicKey);
        String sign = AESCipher.aesEncryptString(param,apiKey);
        callback.invoke(apiKeyEnc,sign);
    }

    /**
     * 用于单个参数加密  例如 密码
     * @param para
     * @param callback
     * @throws Exception
     */
    @ReactMethod
    public void privateEncrypt(String para,Callback callback) throws Exception {
        callback.invoke(URLEncoder.encode(RSA.encrypt(para,publicKey), "utf-8"));
    }

    @ReactMethod
    public void promise(String msg, Promise promise) {
        try {
            Toast.makeText(context,msg,Toast.LENGTH_SHORT).show();
            promise.resolve("已经成功弹出了土司");
        } catch (Exception e) {
            e.printStackTrace();
            promise.resolve(e.toString());
        }
    }

    public void sendMessageToRn(String msg){
        Log.i("wangyu", msg);
        context.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
                .emit("Android",msg);
    }

    @Override
    public void onActivityResult(Activity activity, int requestCode, int resultCode, Intent data) {
        sendMessageToRn("先回调了这里");
    }

    @Override
    public void onNewIntent(Intent intent) {

    }

    @Override
    public void onHostResume() {

    }

    @Override
    public void onHostPause() {

    }

    @Override
    public void onHostDestroy() {

    }
}
